package com.hliushi.state.example01_wrong;

/**
 * 交通灯类
 *
 * @author: hliushi
 * @date: 2023/2/15 22:34
 **/
public class TrafficLight {

    /**
     * 交通灯有红灯(禁行)、黄灯(警告)、绿灯(通行) 三种状态
     */
    private Type state = Type.RED;


    /**
     * 切换为绿灯(通行)状态
     */
    public void switchToGreen() {
        if (Type.GREEN.equals(state)) {
            throw new UnsupportedOperationException("已经是绿灯状态无须再切换!!!");
        } else if (Type.RED.equals(state)) {
            throw new UnsupportedOperationException("红灯不能切换为绿灯");
        } else if (Type.YELLOW.equals(state)) {
            state = Type.GREEN;
            System.out.println("OK... 绿灯亮起30秒");
        }
    }


    public void switchToRed() {
        if (Type.GREEN.equals(state)) {
            throw new UnsupportedOperationException("绿灯不能切换为红灯");
        } else if (Type.RED.equals(state)) {
            throw new UnsupportedOperationException("已经是红灯状态无须再切换!!!");
        } else if (Type.YELLOW.equals(state)) {
            state = Type.RED;
            System.out.println("OK... 红灯亮起60秒");
        }
    }

    public void switchToYellow() {
        if (Type.YELLOW.equals(state)) {
            throw new UnsupportedOperationException("已经是黄灯状态无须再切换");
        } else if (Type.GREEN.equals(state) || Type.RED.equals(state)) {
            state = Type.YELLOW;
            System.out.println("OK... 黄灯亮起5秒");
        }
    }

    static enum Type {
        RED, YELLOW, GREEN;
    }
}
